<?php

namespace app\admin\controller;

use app\common\model\GoodsGroup as GoodsGroupModel;
use app\common\model\GoodsGroupPerson as GoodsGroupPersonModel;
use app\common\model\Order as OrderModel;
use app\common\model\Goods as GoodsModel;
use app\common\model\GoodsOption as GoodsOptionModel;
use think\Db;
/**
 * 拼团控制器
 * Class Goodsgroup
 * @package app\index\controller
 */
class Goodsgroup extends Base
{
    /**
     * [getIndex  拼团失败]
     * @param timetype 1下单时间 2付款时间 3出行时间 4所有时间
     * @return json
     * @author [C。]
     */
    public function Index()
    {
        $param = input('param.');
        $model = new GoodsGroupModel;
        $gmodel = new GoodsModel;
        $gomodel = new GoodsOptionModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try {
            if(!empty($param['paytype'])){
                $map['o.paytype'] = $param['paytype'];
            }
            if(!empty($param['timetype'])){
                if($param['timetype'] == 1){
                    $map['o.create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
                }
                if($param['timetype'] == 2){
                    $map['o.paytime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
                }
                if($param['timetype'] == 3){
                    $map['o.gotime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
                }
                if($param['timetype'] == 4){
                    $map['o.gotime|o.create_time|o.paytime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
                }
            }
            if(!empty($param['contype'])){
                if($param['contype'] == 1){
                    $map['o.ordersn'] = $param['con'];
                }
                if($param['contype'] == 2){
                    $map['g.title'] = $param['con'];
                }
            }
            $map['g.addtime'] = ['lt',time()-24*3600*config('home.cancel_time')];
            $map['o.status'] = 1;
            $map['g.status'] = ['neq',2];
            $map['o.refundid'] = 0;
            $map['go.person_num'] = ['exp',Db::raw('>g.num')];
            $field = 'o.id,o.optionid,o.memid,o.create_time,o.ordersn,o.price,g.num,o.name,o.tel,o.status,o.people,o.gotime,o.paytype,go.person_num,go.day,o.goodsid';
            $count = $model->getrefundcount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getrefund($map,$this->from, $this->size,$field);
            foreach($lists as &$v) {
                $option = $gomodel->findData($v['optionid'],'id','marketprice');
                $v['people'] = json_decode(unserialize($v['people']));
                $v['gotime'] = date('Y-m-d', $v['gotime']);
                $goods = $gmodel->findData($v['goodsid'],'id','cover');
                $cover = explode(',',$goods['cover']);
                $v['cover'] = $cover[0];
                $v['state'] = "拼团失败";
                $v['goodsprice'] = $option['marketprice'];
            }
        } catch (\Exception $e) {
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        writelog('查看拼团失败订单',1,$user);
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [findgroup  根据出行时间获取拼团]
     * @return json
     * @author [C。]
     */
    public function findgroup()
    {
        $param = input('param.');
        $gpmodel = new GoodsGroupModel();
        $omodel = new OrderModel;
        $map = [];
        $this->getPageAndSize($param);
        $user = $this->user;
        try{
            $map['ao.gotime'] = strtotime($param['gotime']);
            $map['g.addtime'] = ['between',[time() - 24 * 3600 * config('home.cancel_time'),time() + 24 * 3600 * config('home.cancel_time')]];
            $map['o.person_num'] = ['exp',Db::raw('>g.num')];
            $map['o.status'] = 1;
            $map['o.goodsid'] = $param['goodsid'];
            $lists = $gpmodel->getLists($map,'g.id,o.goodsid,m.img,m.username,o.person_num,o.day,g.addtime,g.num');

            foreach($lists as &$v){
                $v['residue'] = 24*3600*config('home.cancel_time') - (time() - $v['addtime']);
                $v['addtime'] = date('Y-m-d',$v['addtime']);
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$v['img'];
                }
                $map2['status'] = 0;
                $map2['groupid'] = $v['id'];
                $map2['create_time'] = ['gt',time()-15*60];
                $num = $omodel->getSum($map2,'num');
                $v['hasnum'] = $v['num'] + $num;
                if($v['person_num'] <= $num + $v['num']){
                    $v['isshow'] = 0;
                }else{
                    $v['isshow'] = 1;
                }
            }
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        writelog('根据出行日期查看拼团',1,$user);
        return admin_json(1,'获取成功', $lists);
    }
    /**
     * [togroup  拼团]
     * @return json
     * @author [C。]
     */
    public function togroup()
    {
        $param = input('param.');
        $model = new OrderModel;
        $gpmodel = new GoodsGroupPersonModel;
        $user = $this->user;
        try{
            $order = $model->findData($param['id'],'id','num');
            $hasnum = $param['hasnum'] + $order['num'];
            if($hasnum > $param['option_personnum']){
                return admin_json(-1, '该笔订单人数超出剩余可拼人数！');
            }
            $groupperson = $gpmodel->findData($param['id'],'orderid','id');
            $data = array(
                'id' => $groupperson['id'],
                'group_id' => $param['group_id'],
            );
            $gpmodel->editData($data);
            $param['gotime'] = strtotime($param['gotime']);
            $param['type'] = 3;
            $model->editData($param);
        }catch(\Exception $e){
            return admin_json(-1, '拼团失败'.$e->getMessage());
        }
        writelog('执行拼团',1,$user);
        return admin_json(1, '拼团成功');
    }
}